/*
 * @Descripttion:
 * @version:
 * @Date: 2021-04-20 11:06:21
 * @LastEditors: huzhushan@126.com
 * @LastEditTime: 2022-09-27 14:57:06
 * @Author: huzhushan@126.com
 * @HomePage: https://huzhushan.gitee.io/vue3-element-admin
 * @Github: https://github.com/huzhushan/vue3-element-admin
 * @Donate: https://huzhushan.gitee.io/vue3-element-admin/donate/
 */
import { defineStore } from 'pinia'
import { GetUserinfo } from '@/api/login'

export const useAccount = defineStore('account', {
  state: () => ({
    userinfo: null,
    permissionList: [],
  }),
  actions: {
    // 清除用户信息
    clearUserinfo() {
      this.userinfo = null
    },
    // 获取用户信息
    async getUserinfo() {
      const { code, data } = await GetUserinfo()
      //用户信息存储到localStorage
      window.localStorage.setItem('un', data.un)
      window.localStorage.setItem('avatar', data.avatar)
      window.localStorage.setItem('phone', data.phone)
      window.localStorage.setItem('id', data.id)
      // 信用等级
      window.localStorage.setItem('credit', data.credit)
      // 资产评级
      window.localStorage.setItem('asset', data.asset)
      window.localStorage.setItem('id', data.id)
      //真实姓名
      window.localStorage.setItem('realname', data.realname)
      //身份证号
      window.localStorage.setItem('idcard', data.idcard)
      //用户id
      window.localStorage.setItem('id', data.id)
      //密码
      window.localStorage.setItem('password', data.password)
      window.localStorage.setItem('career', data.career)
      //性别
      window.localStorage.setItem('sex', data.sex)
      if (+code === 200) {
        this.userinfo = data
        return Promise.resolve(data)
      }
    },
  },
})
